<?php
namespace Admin\Frontend\Controllers;

use Admin\Models\homeset\XhhomeSet;
use Admin\Models\homeset\XhUserSet;
use Helpers\Main;
use Helpers\User;
use Helpers\UserPower;
use Helpers\Assets;

class HomeController extends ControllerBase
{
//    private $windows = [
//        [
//            'url'   => 'pm/schedule/homeSchedule',
//            'title' => '我的日程',
//            'power' => 'Bpm_Schedule'
//        ],
//        [
//            'url'   => 'Msg/home',
//            'title' => '通知公告',
//        ],
//        [
//            'url'   => 'Track/home',
//            'title' => '问题跟踪',
//            'power' => 'Track'
//        ],
//        [
//            'url'   => 'Article/home',
//            'title' => '信息发布',
//            'power' => 'Article'
//        ],
//        [
//            'url'   => 'Tch/home',
//            'title' => '公开课',
//            'power' => 'School_Teacher_PublicClass'
//        ],
//        [
//            'url'   => 'school_book/home',
//            'title' => '校本库',
//            'power' => 'School_List_Books'
//        ],
//        [
//            'url'   => 'pm/project/home',
//            'title' => '项目管理',
//            'power' => 'Bpm_Project'
//        ],
//
//    ];

    public function indexAction()
    {
        //教师且非员工跳转到学情
//        if (Main::getLoginUserInfo('userType') == 4 && !User::isStaff()) {
//            return $this->response->redirect('manage/knowledge/index.html?schoolId=' . Main::getLoginUserInfo('schoolId'));
//        }
        $this->view->pick('home/index');
        Assets::importMainJs('assets/xh/xhTop.js');
        Assets::importJs('../short/js/short.js');

        $userId = Main::getLoginUserInfo('userId');
        if (User::isStaff() || User::isAdmin()) {
            $windows = XhUserSet::findFirst([
                'conditions' => 'bDelete = 0 and iUserId = :iUserId:',
                'bind' => ['iUserId' => $userId]
            ]);
            if (!$windows) {
                $windows = XhUserSet::findFirst([
                    'conditions' => 'bDelete = 0 and iUserId = 0',
                ]);
            }
            $modelList = json_decode($windows->sModelList, true);

            $data = [];
            foreach ($modelList as $window) {
                $modelInfo = XhhomeSet::findFirst([
                    'conditions' => 'bDelete = 0 and iId = :iId:',
                    'bind' => ['iId' => $window['model_id']]
                ]);

                $isShow = $window['is_show'];

                if ($isShow && $modelInfo) {
                    $title = $modelInfo->sName;
                    $url = $modelInfo->sUrl;
                    $method = $modelInfo->sGetMethod;
                    if (isset($power)) {
                        if (UserPower::checkUserPower($power)) {
                            $data[] = [
                                'title' => $title,
                                'url' => $url,
                                'method' => $method
                            ];
                        }
                    } else {
                        $data[] = [
                            'title' => $title,
                            'url' => $url,
                            'method' => $method
                        ];
                    }
                }
            }
        }

        return $this->view->setVar('windows', $data);
    }
}
